angular.module('service.shop', [
        "ngResource"
    ])
    .service('ShopService', ShopService);
ShopService.$inject = ['$resource'];
function ShopService($resource) {
    var shopres = $resource(mallurl + 'shops/:shopid', {}, {
        get: {cache: true, method: 'GET'}
    });
    var shop_map = {};
    //获取县镇商店菜单
    this.selectAllShops = function () {
        //var ShopList= $resource(mallurl + 'shops');
        return shopres.query();
    };

    //获取商店(优先从缓存读取)
    this.queryShopByShopid = function (shopid) {
        if (shop_map[shopid]) {
            return shop_map[shopid];
        }
        return shopres.get({shopid: shopid});
    };

    //获取分组商店菜单
    this.queryShoplistByGroup = function (groupid) {
        var ShopByGroup = $resource(mallurl + 'shoplist/bytag/:id', {}, {
            get: {cache: true, method: 'GET'},
            query: {cache: true, method: 'GET', isArray: true}
        });
        return ShopByGroup.query({id: groupid});
    };
    //缓存店铺名称
    /*this.saveShopByStorage = function(cshop){
     setCookie('shopName',cshop.name,1);
     setCookie('shopId',cshop.shopid,1);
     }*/
    //获取店铺详情
    this.getShopByShopId = function(shopId){
        //var shopById = $resource(mallurl + 'shops/:shopid');
        return shopres.get({shopid:shopId});
    };
    //读取缓存店铺名称
    /*this.getShopByStorage = function(){
     var shop = {};
     if(getCookie('shopName')!=null&&getCookie('shopId')!=null){
     shop.name = getCookie('shopName');
     shop.shopid = getCookie('shopId');
     }
     return shop;
     }*/
    //通过uid获取推荐店铺
    this.get_cshop = function (uid,state) {
        var resource = $resource(mallurl + "shopsoperator/uid/:uid", {}, {
            query: {cache: true, method: 'GET', isArray: true}
        });
        return resource.query({uid: uid,state:state});
    };
    //通过shopid获取店铺属性
    this.getpropertyByshopid = function(shopid){
        var resource = $resource(mallurl + "shopsproperty/:shopid");
        return resource.get({shopid:shopid})
    };
    //通过goodid获取销量
    this.getgoodspropertyBygoodsid = function(goodsid,success,error){
        var resource = $resource(mallurl + "goodsproperty/:goodsid");
        return resource.get({goodsid:goodsid},success,error)
    };
    //获取区域商店
    //根据区域获得商店
    this.getareaByscope = function(lng,lat){
        var resource = $resource(mallurl + "shoplist/byscope/:longitude/:latitude");
        return resource.query({longitude:lng,latitude:lat})
    };
    //通过搜索条件获取店铺
    this.getBySearch = function(search){
        var history_list = $cookies.get("history_search");
        var hasName = false;
        //遍历历史搜索数组没有重复搜索名字才允许存入缓存数组中
        if(history_list){
            for (var i = 0; i < history_list.length; i++) {
                if (search.q == history_list[i]) {
                    hasName = true;
                    break;
                }
            }
        }else{
            history_list = [];
        }
        if (!hasName) {
            if (search.q != '') {
                if (history_list.length == 6) {
                    history_list.splice(5, 1);
                }
                history_list.unshift(search.q);
                $cookies.put("history_search", history_list);
            }
        }
        var resource = $resource(mallurl + "shoplist/bysearch");
        return resource.query(search);
    };

    //根据id获取店铺配送信息
    this.getScopeById = function(shopid){
        var resource = $resource(mallurl + "shopsscope/:shopid");
        return resource.get({shopid:shopid})
    };
    //通过当前位置判断推荐店铺是否可以配送
    this.judgeNearShop = function(shoplist,location){
        for(var i=0;i<shoplist.length;i++){
            rayCasting(location, shoplist[i])
        }
    };
    //店铺距离计算
    function rayCasting(location, shop) {
        var polyList = JSON.parse(shop.deliveryScope);
        if(!polyList){
            return shop.scope = false;
        }
        var px = location[0],
            py = location[1],
            flag = false;
        for(var k=0;k<polyList.length;k++){
            var poly = polyList[k];
            for(var i = 0, l = poly.length, j = l - 1; i < l; j = i, i++) {
                var sx = poly[i].x,
                    sy = poly[i].y,
                    tx = poly[j].x,
                    ty = poly[j].y;
                // 点与多边形顶点重合
                if((sx === px && sy === py) || (tx === px && ty === py)) {
                    return shop.scope = true;
                }

                // 判断线段两端点是否在射线两侧
                if((sy < py && ty >= py) || (sy >= py && ty < py)) {
                    // 线段上与射线 Y 坐标相同的点的 X 坐标
                    var x = sx + (py - sy) * (tx - sx) / (ty - sy);

                    // 点在多边形的边上
                    if(x === px) {
                        return shop.scope = true;
                    }

                    // 射线穿过多边形的边界
                    if(x > px) {
                        flag = !flag
                    }
                }
            }
            if(flag){
                break;
            }
        }
        // 射线穿过多边形边界的次数为奇数时点在多边形内
        return shop.scope= flag;
    }
    //获取商店菜单分类
    this.queryShopmenuitems = function (groupId ,success,error) {
        var Shopmenuitems = $resource(mallurl + 'menu/bygroup/:shopid', {}, {
            query: {cache: true, method: 'GET', isArray: true}});
        return Shopmenuitems.query({shopid: groupId },success,error);
    };
    //店铺tags位置判断
    var tagType;
    this.setTag = function(tag){
        tagType = tag;
    };
    this.getTag = function(){
        return tagType;
    }
}